package com.zg.scf.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.R;
import com.zg.common.core.dao.scf.entity.Product;
import com.zg.scf.domain.convert.ProductConvert;
import com.zg.scf.domain.req.product.ProductAddReq;
import com.zg.scf.domain.req.product.ProductEditReq;
import com.zg.scf.domain.req.product.ProductPageReq;
import com.zg.scf.domain.req.StateReq;
import com.zg.scf.domain.vo.ProductVo;
import com.zg.scf.service.ProductService;
import com.zg.scf.enm.StateEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/5/22 10:54
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/scf/product")
@Slf4j
@Api(tags = "产品")
public class ProductController {

    private final ProductService productService;

    private final ProductConvert productConvert;


    @PostMapping("/page")
    @ApiOperation(value = "分页")
    public R<Page<ProductVo>> page(@RequestBody ProductPageReq pageReq) {
        Page<ProductVo> page = productService.page(pageReq,ProductVo.class);
        return R.success(page);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public R add(@RequestBody @Valid ProductAddReq addReq) {
        addReq.setState(StateEnum.N.getValue());
        productService.add(addReq);
        return R.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改")
    public R edit(@RequestBody @Valid ProductEditReq editReq) {
        productService.edit(editReq);
        return R.success();
    }

    @PostMapping("/editState")
    @ApiOperation(value = "修改状态")
    public R editState(@RequestBody @Valid StateReq editReq) {
        productService.editState(editReq);
        return R.success();
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除")
    public R remove(@RequestBody List<Long> ids) {
        productService.remove(ids);
        return R.success();
    }

    @GetMapping("/listByCredit")
    @ApiOperation(value = "获取授信的产品列表")
    public R<List<ProductVo>> listByCredit(@Param("id") Long id) {
        List<Product> list=productService.listByCredit(id);
        return R.success(productConvert.toVoList(list));
    }

    @GetMapping("/generateCode")
    @ApiOperation(value = "产品编号")
    public R<String>  generateCode(Long bankId){
        return R.success(productService.generateCode(bankId));
    }
}
